import 'package:finance_app/src/entities/chat_room_entity.dart';
import 'package:finance_app/src/entities/chat_room_member_entity.dart';

chatRoomEntityFromJson(ChatRoomEntity data, Map<String, dynamic> json) {
	if (json['data_type'] != null) {
		data.dataType = json['data_type'].toString();
	}
	if (json['id'] != null) {
		data.id = json['id'] is String
				? int.tryParse(json['id'])
				: json['id'].toInt();
	}
	if (json['company_id'] != null) {
		data.companyId = json['company_id'] is String
				? int.tryParse(json['company_id'])
				: json['company_id'].toInt();
	}
	if (json['parent_chat_room_id'] != null) {
		data.parentChatRoomId = json['parent_chat_room_id'];
	}
	if (json['type'] != null) {
		data.type = json['type'].toString();
	}
	if (json['name'] != null) {
		data.name = json['name'].toString();
	}
	if (json['current_id'] != null) {
		data.currentId = json['current_id'] is String
				? int.tryParse(json['current_id'])
				: json['current_id'].toInt();
	}
	if (json['last_read_id'] != null) {
		data.lastReadId = json['last_read_id'] is String
				? int.tryParse(json['last_read_id'])
				: json['last_read_id'].toInt();
	}
	if (json['created_at'] != null) {
		data.createdAt = json['created_at'].toString();
	}
	if (json['updated_at'] != null) {
		data.updatedAt = json['updated_at'].toString();
	}
	if (json['members'] != null) {
		data.members = (json['members'] as List).map((v) => ChatRoomMemberEntity().fromJson(v)).toList();
	}
	return data;
}

Map<String, dynamic> chatRoomEntityToJson(ChatRoomEntity entity) {
	final Map<String, dynamic> data = new Map<String, dynamic>();
	data['data_type'] = entity.dataType;
	data['id'] = entity.id;
	data['company_id'] = entity.companyId;
	data['parent_chat_room_id'] = entity.parentChatRoomId;
	data['type'] = entity.type;
	data['name'] = entity.name;
	data['current_id'] = entity.currentId;
	data['last_read_id'] = entity.lastReadId;
	data['created_at'] = entity.createdAt;
	data['updated_at'] = entity.updatedAt;
	data['members'] =  entity.members?.map((v) => v.toJson())?.toList();
	return data;
}